/*
 * Copyright 2002-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.loooooo.pm2024.security.authz.permission;



import com.loooooo.pm2024.security.authz.Permission;

import java.io.Serializable;
import java.util.*;

/**
 * @author ytx98
 * @date 2018年7月26日 新建
 *
 */
public class WildcardPermission implements Permission, Serializable {

	protected static final String WILDCARD_TOKEN = "*";
	protected static final String PART_DIVIDER_TOKEN = ":";
	protected static final String SUBPART_DIVIDER_TOKEN = ",";
	protected static final boolean DEFAULT_CASE_SENSITIVE = false;

	private List<Set<String>> parts;

	protected WildcardPermission() {
	}

	public WildcardPermission(String wildcardString) {
		this(wildcardString, DEFAULT_CASE_SENSITIVE);
	}

	public WildcardPermission(String wildcardString, boolean caseSensitive) {
		setParts(wildcardString, caseSensitive);
	}

	protected void setParts(String wildcardString) {
		setParts(wildcardString, DEFAULT_CASE_SENSITIVE);
	}

	protected void setParts(String wildcardString, boolean caseSensitive) {
		// wildcardString = StringUtils.clean(wildcardString);

		if (wildcardString == null || wildcardString.isEmpty()) {
			throw new IllegalArgumentException(
					"Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.");
		}

		if (!caseSensitive) {
			wildcardString = wildcardString.toLowerCase();
		}

		List<String> parts = Arrays.asList(wildcardString.split(PART_DIVIDER_TOKEN));// CollectionUtils.asList(wildcardString.split(PART_DIVIDER_TOKEN));

		this.parts = new ArrayList();// new ArrayList<Set<String>>();
		for (String part : parts) {
			Set<String> subparts = new HashSet();
			subparts.addAll(Arrays.asList(part.split(SUBPART_DIVIDER_TOKEN)));
			// CollectionUtils.asSet(part.split(SUBPART_DIVIDER_TOKEN));

			if (subparts.isEmpty()) {
				throw new IllegalArgumentException(
						"Wildcard string cannot contain parts with only dividers. Make sure permission strings are properly formatted.");
			}
			this.parts.add(subparts);
		}

		if (this.parts.isEmpty()) {
			throw new IllegalArgumentException(
					"Wildcard string cannot contain only dividers. Make sure permission strings are properly formatted.");
		}
	}

	/*--------------------------------------------
	|  A C C E S S O R S / M O D I F I E R S    |
	============================================*/
	protected List<Set<String>> getParts() {
		return this.parts;
	}

	/**
	 * Sets the pre-split String parts of this <code>WildcardPermission</code>.
	 * 
	 * @since 1.3.0
	 * @param parts
	 *            pre-split String parts.
	 */
	protected void setParts(List<Set<String>> parts) {
		this.parts = parts;
	}

	/*--------------------------------------------
	|               M E T H O D S               |
	============================================*/

	public boolean implies(Permission p) {
		// By default only supports comparisons with other WildcardPermissions
		if (!(p instanceof WildcardPermission)) {
			return false;
		}

		WildcardPermission wp = (WildcardPermission) p;

		List<Set<String>> otherParts = wp.getParts();

		int i = 0;
		for (Set<String> otherPart : otherParts) {
			// If this permission has less parts than the other permission, everything after
			// the number of parts contained
			// in this permission is automatically implied, so return true
			if (getParts().size() - 1 < i) {
				return true;
			} else {
				Set<String> part = getParts().get(i);
				if (!part.contains(WILDCARD_TOKEN) && !part.containsAll(otherPart)) {
					return false;
				}
				i++;
			}
		}

		// If this permission has more parts than the other parts, only imply it if all
		// of the other parts are wildcards
		for (; i < getParts().size(); i++) {
			Set<String> part = getParts().get(i);
			if (!part.contains(WILDCARD_TOKEN)) {
				return false;
			}
		}

		return true;
	}

	public String toString() {
		StringBuilder buffer = new StringBuilder();
		for (Set<String> part : parts) {
			if (buffer.length() > 0) {
				buffer.append(PART_DIVIDER_TOKEN);
			}
			Iterator<String> partIt = part.iterator();
			while (partIt.hasNext()) {
				buffer.append(partIt.next());
				if (partIt.hasNext()) {
					buffer.append(SUBPART_DIVIDER_TOKEN);
				}
			}
		}
		return buffer.toString();
	}

	public boolean equals(Object o) {
		if (o instanceof WildcardPermission) {
			WildcardPermission wp = (WildcardPermission) o;
			return parts.equals(wp.parts);
		}
		return false;
	}

	public int hashCode() {
		return parts.hashCode();
	}

	public static void main(String[] args) {
		Permission p1 = new WildcardPermission("aa:*");
		Permission p2 = new WildcardPermission("aa:xx");
		System.out.println(p1.implies(p2));
	}
}
